//
//  LSYplaceChooseView.m
//  contact
//
//  Created by lsy on 15/11/23.
//  Copyright © 2015年 刘世玉. All rights reserved.
//

#import "LSYplaceChooseView.h"
#import <Masonry.h>


#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height







@interface LSYplaceChooseView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *provinceArr;
@property (nonatomic, retain) NSMutableArray *cityArr;
@property (nonatomic, retain) NSMutableArray *zoneArr;
@property (nonatomic, retain) UITableView *provinceTableView;
@property (nonatomic, retain) UITableView *cityTableView;
@property (nonatomic, retain) UITableView *zoneTableView;



@end


@implementation LSYplaceChooseView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.map = [[LSYmapView alloc] init];
        [self addSubview:self.map];
       
        [self createView];
       
    }
    return self;
}


- (void)createView
{
    
   
    NSString *path = @"/Users/lsy13/homeWork_43/contact/contact/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
    self.provinceArr = [NSMutableArray array];
    
    for (NSString *temp in strArr) {
        if (![temp hasPrefix:@"  "]) {
            NSMutableDictionary *pDic = [NSMutableDictionary dictionary];
            [pDic setObject:temp forKey:@"province"];
            NSMutableArray *cArr = [NSMutableArray array];
            [pDic setObject:cArr forKey:@"city"];
            [self.provinceArr addObject:pDic];
        } else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]) {
            NSMutableDictionary *cDic = [NSMutableDictionary dictionary];
            [cDic setObject:temp forKey:@"city"];
            NSMutableArray *zArr = [NSMutableArray array];
            [cDic setObject:zArr forKey:@"zone"];
            NSMutableDictionary *pDic = self.provinceArr.lastObject;
            NSMutableArray *cArr = pDic[@"city"];
            [cArr addObject:cDic];
        } else {
            NSMutableDictionary *pDic = self.provinceArr.lastObject;
            NSMutableArray *cArr = pDic[@"city"];
            NSMutableDictionary *cDic = cArr.lastObject;
            NSMutableArray *zArr = cDic[@"zone"];
            [zArr addObject:temp];
        }
        
    }
   
    NSData *data = [NSJSONSerialization dataWithJSONObject:self.provinceArr options:NSJSONWritingPrettyPrinted error:nil];
    NSString *p = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",p);

    
    self.map.frame = CGRectMake(0, HEIGHT / 2, WIDTH, HEIGHT / 2);
    self.map.backgroundColor = [UIColor blueColor];
    [self getView];
    
}

- (void)getView
{
    // Do any additional setup after loading the view.
    //    automaticallyAdjustsScrollViewInsets 会把第一个滚动视图的坐标起始位置设置成(0, 64),但是它不会管后面的滚动视图, 所以想要保留半透明效果需要把这个属性设置为 NO;
    
    //    或者把导航栏设置成不透明, 坐标系就稳定了
    
    
    
    self.provinceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH / 3, HEIGHT / 2 - 64) style:UITableViewStylePlain];
    [self addSubview:self.provinceTableView];
    self.provinceTableView.delegate = self;
    self.provinceTableView.dataSource = self;
    
    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3, HEIGHT/ 2 - 64) style:UITableViewStylePlain];
    [self addSubview:self.cityTableView];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    
    self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH, HEIGHT / 2 - 64) style:UITableViewStylePlain ];
    [self addSubview:self.zoneTableView];
    self.zoneTableView.delegate = self;
    self.zoneTableView.dataSource = self;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    点击省出现市
    if (tableView == self.provinceTableView) {
        //        把省对应的市数组给属性赋值
        self.cityArr = self.provinceArr[indexPath.row][@"city"];
        self.zoneArr = nil;
        [self.cityTableView reloadData];
        [self.zoneTableView reloadData];
        
    } else if (tableView == self.cityTableView) {
        self.zoneArr = self.cityArr[indexPath.row][@"zone"];
        [self.zoneTableView reloadData];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"还点!还点!没了!!" delegate:nil cancelButtonTitle:@"额" otherButtonTitles:nil, nil];
        [alert show];
    }

}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.provinceTableView) {
        return self.provinceArr.count;
    } else if (tableView == self.cityTableView) {
        return self.cityArr.count;
    } else {
        return self.zoneArr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.provinceTableView) {
        static NSString *provinceReuse = @"provinceReuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:provinceReuse];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:provinceReuse];
        }
        cell.textLabel.text = self.provinceArr[indexPath.row][@"province"];
        return cell;
    } else if (tableView == self.cityTableView) {
        static NSString *cityReuse = @"cityReuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityReuse];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cityReuse];
        }
        cell.textLabel.text = self.cityArr[indexPath.row][@"city"];
        return cell;
    } else {
        static NSString *zoneReuse = @"zoneReuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zoneReuse];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:zoneReuse];
        }
        cell.textLabel.text = self.zoneArr[indexPath.row];
        return cell;
    }
    
    
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
